﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Text;
using System.Drawing.Drawing2D;
using System.Collections.Generic;
using System.Text;
using System.Xml;
//using LetiLib.GUI;

namespace LetiLib.Data
{
    public class TextProgram : BaseProgram
    {
        // Text info
        public string Content { get; set; }
        public string FontName { get; set; }
        public int FontSize { get; set; }
        public FontStyle FontStyle { get; set; }
        public Point Position { get; set; }
        private Color _color;
        public Color Color
        {
            get { return _color; }
            set
            {
                if (value == Color.Green)
                {
                    _color = Color.Lime;
                }
                else if (value == Color.Red || value == Color.Yellow)
                {
                    _color = value;
                }
                else
                {
                    _color = Color.Gray;
                }
            }
        }
        public int Width { get; set; }
        public int Height { get; set; }
         /// <summary>
        /// Default constructor
        /// </summary>
        /// <returns></returns>
        public TextProgram():base(0,PROGRAM_TYPE.TEXT)
        {

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="fontName"></param>
        /// <param name="fontSize"></param>
        /// <param name="fontStyle"></param>
        /// <param name="pos"></param>
        /// <returns></returns>
        public TextProgram(ushort id,string content,Color color,string fontName, int fontSize,FontStyle fontStyle,Point pos)
            : base(id,PROGRAM_TYPE.TEXT)
        {
            Content = content;
            Color = color;
            FontName = fontName;
            FontSize = fontSize;
            FontStyle = fontStyle;
            Position = pos;
        }

        /*
        public ProgramData GenerateProgramDataWithLed(LETILed letiLed)
        {
            DataInfo info = new DataInfo((ushort)letiLed.Resolution.Width,
                (ushort)letiLed.Resolution.Height, LETILed.BPP);
            return new ProgramData(info, letiLed.getBytes());
        }
        */
        #region | IBaseProgram implementation |
        public override Bitmap CreateBitmapImage()
        {
            // Create bitmap Object.
            Bitmap objBmpImage = new Bitmap(8, 8);
            // Create variables.
            int intWidth = 0;
            int intHeight = 0;

            // Create the Font object for the image text drawing.
            Font objFont = new Font(FontName, FontSize, FontStyle, System.Drawing.GraphicsUnit.Point);

            // Create a graphics object to measure the text's width and height.
            Graphics objGraphics = Graphics.FromImage(objBmpImage);

            // This is where the bitmap size is determined.
            intWidth = (int)objGraphics.MeasureString(Content, objFont).Width + Position.X;
            intHeight = (int)objGraphics.MeasureString(Content, objFont).Height + Position.Y;


            // Check if sImageText = null
            if (intWidth == 0 || intHeight == 0)
            {
                intHeight = 1;
                intWidth = 1;
            }

            // Create the bmpImage again with the correct size for the text and font.
            objBmpImage = new Bitmap(objBmpImage, intWidth, intHeight);
            //objBmpImage = new Bitmap(objBmpImage, new Size(intWidth, intHeight));
            // Add the colors to the new bitmap.
            objGraphics = Graphics.FromImage(objBmpImage);

            // Set Background color
            objGraphics.Clear(Color.White);

            // 
            objGraphics.SmoothingMode = SmoothingMode.AntiAlias;
            objGraphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit;

            // Draw string to Image
            objGraphics.DrawString(Content, objFont, new SolidBrush(Color), Position);

            // Flush
            objGraphics.Flush();

            // return
            return (objBmpImage);
        }

        public override string ToXML()
        {
            string xmlContent = "<Program></Program>";
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlContent);
            XmlNode newNode = doc.DocumentElement;
            XmlAttribute nameAttribute = doc.CreateAttribute("Name");
            nameAttribute.Value = ID.ToString();
            newNode.Attributes.Append(nameAttribute);

            XmlAttribute typeAttribute = doc.CreateAttribute("Type");
            typeAttribute.Value = Enum.GetName(typeof(PROGRAM_TYPE), Type);
            newNode.Attributes.Append(typeAttribute);

            System.Diagnostics.Debug.WriteLine(newNode.OuterXml);
            return newNode.OuterXml;
        }
        #endregion
    }
}
